package com.mkb.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mkb.dao.GraphUserDAO;
import com.mkb.entity.GraphUserDO;
import com.mkb.entity.GraphUserDTO;
import com.mkb.service.GraphUserService;
import com.mkb.utils.MapUtils;
import com.zz.common.util.ObjectUtils;
import com.zz.common.validator.Assert;
import com.zz.common.validator.ValidatorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class GraphUserServiceImpl implements GraphUserService {
	@Autowired
	private GraphUserDAO graphUserDAO;

	@Override
	public Boolean deleteById(Long id) {
		graphUserDAO.deleteById(id);
		return true;
	}

	@Override
	public Boolean deleteByGraphId(Long graphId) throws Exception {
		graphUserDAO.deleteByGraphId(graphId);
		return true;
	}

	@Override
	public Boolean create(GraphUserDTO record) throws Exception {
		GraphUserDO entity = record.clone(GraphUserDO.class);
		graphUserDAO.insert(entity);
		return true;
	}

	@Override
	public Boolean createBatch(List<GraphUserDTO> records) throws Exception {
		graphUserDAO.insertBatch(ObjectUtils.convertList(records, GraphUserDO.class));
		return true;
	}

	@Override
	public GraphUserDTO getById(Long id) throws Exception{
		Assert.isNull(id, "id 不能为空");
		GraphUserDO entity= graphUserDAO.getById(id);
		Assert.isNull(entity, "对象不存在");
		GraphUserDTO dto = entity.clone(GraphUserDTO.class);
		return dto;
	}

	@Override
	public Boolean update(GraphUserDTO record) throws Exception {
		Assert.isNull(record, "对象不存在");
		Assert.isNull(record.getId(), "对象id不存在");
		GraphUserDO entity = record.clone(GraphUserDO.class);
		graphUserDAO.update(entity);
		return true;
	}

	@Override
	public PageInfo<GraphUserDTO> queryPage(GraphUserDTO graphUserDTO) throws Exception {
	        ValidatorUtils.validateProperty(graphUserDTO, "pageNum", "pageSize");
		PageHelper.startPage(graphUserDTO.getPageNum(), graphUserDTO.getPageSize());
		List<GraphUserDO> graphUserDOS = graphUserDAO.queryList(MapUtils.parseMap(graphUserDTO));
		List<GraphUserDTO> graphUserDTOList = ObjectUtils.convertList(graphUserDOS, GraphUserDTO.class);
		return new PageInfo<>(graphUserDTOList);
	}

	@Override
	public List<GraphUserDTO> queryList(GraphUserDTO graphUserDTO) throws Exception {
		List<GraphUserDO> graphUserDOS = graphUserDAO.queryList(MapUtils.parseMap(graphUserDTO));
		List<GraphUserDTO> graphUserDTOList = ObjectUtils.convertList(graphUserDOS, GraphUserDTO.class);
		return graphUserDTOList;
	}

	@Override
	public int count(Map<String, Object> map) throws Exception {
		return graphUserDAO.count(map);
	}

}